# -*- coding: utf-8 -*-
import sys
import os
import gobject

from threading import Thread, Condition
import time




class InitialInfoSender(Thread):
    def __init__(self, plugin, buddyList):
        Thread.__init__(self)
        self.plugin = plugin
        self.buddyList = buddyList
	self.conditionInfo = Condition()
	self.pmList = []
	self.nickList = []
	
	#TODO: delete and refactoring stuff like this...
	self.pmListName = []
	self.nickListName = []

    def setPersonalMessage(self, contactId, personalMessage):
        print personalMessage
	#self.conditionInfo.acquire()
	self.pmListName.append(contactId)
	self.pmList.append(personalMessage)
	#self.conditionInfo.notify()
        #self.conditionInfo.release()

    def setNickName(self, contactId, nickName):
        print nickName
	self.nickListName.append(contactId)
	self.nickList.append(nickName)

    def __oneByOne(self, listContacts, ListData, setNickPM = True):
	iteratorNames = iter(listContacts)
	iteratorData = iter(ListData)
        listLengh = len(listContacts)
        count = 0
        while count < listLengh:
	    if setNickPM:
		self.plugin.parentalController.setNickName(self.plugin.accountUser, iteratorNames.next(), iteratorData.next())
	    else:
		self.plugin.parentalController.setPersonalMessage(self.plugin.accountUser, iteratorNames.next(), iteratorData.next())
            count += 1

    def run(self):
        onlineList = []
        time.sleep(20)
        for contactId in self.buddyList:
            if not self.plugin.controller.contacts.contacts[contactId].status == 0:
                onlineList.append(contactId)

	self.plugin.boolInfo = False

	self.__oneByOne(self.nickListName, self.nickList, True)
	self.__oneByOne(self.pmListName, self.pmList, False)
        self.plugin.parentalController.setBuddyListStatus(self.plugin.accountUser, onlineList)


class BlockContact():
    def __init__(self, controller):
        self.controller = controller

    def __call__(self, contact):
        #TODO: Do some verification on the block status before doing anything with a contact
        #self.controller.contacts.block(contact)
        self.controller.contacts.set_alias(contact, "[Blocked] "+contact)


class UnblockContact():
    def __init__(self, controller):
        self.controller = controller

    def __call__(self, contact):
        #TODO: Do some verification on the block status before doing anything with a contact
        #self.controller.contacts.unblock(contact)
        self.controller.contacts.set_alias(contact, "")
